package com.android.huimeiqupin.view.group;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.android.huimeiqupin.R;
import com.android.huimeiqupin.model.detail.Banner;
import com.android.huimeiqupin.model.group.AdPictureData;

import java.util.ArrayList;


public class ViewPagerIndicator extends View {
    private Paint paint1;
    private Paint paint2;
    private float cx;
    private float cy;
    private float radius = 10;
    private int count = 0;
    private float offset;
    private ViewPager mViewPager;
    public boolean mDragging;
    public boolean isVisible = true;
    public FragmentManager mFragmentManager;
    private static final int MAX = 100000;
    private BannerAdapter bannerAdapter;
    public ViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint1.setColor(getResources().getColor(R.color.white));
        paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint2.setColor(getResources().getColor(R.color.app_title));
    }
    @Override
    protected void onDraw(Canvas canvas) {
        cx = (int) (getWidth() / 2 - (count - 1) * 1.5 * radius);
        cy = getHeight() / 5 * 4;
        for (int i = 0; i < count; i++) {
            canvas.drawCircle(cx + 4 * radius * i, cy, radius, paint1);
        }
        canvas.drawCircle(cx + offset, cy, radius, paint2);
    }

    private void move(int pos, float x) {
        if (count > 0) {
            if (pos == count - 1) {
                offset = (int) ((pos) * 4 * radius);
            } else {
                offset = (int) ((pos + x) * 4 * radius);
            }
            invalidate();
        }
    }

    /**
     * 设置ViewPager隐藏不滚动，显示滚动，在onStart与onPause调用
     *
     * @param isVisible
     */
    public void setVisible(boolean isVisible) {
        this.isVisible = isVisible;
    }

    /**
     * 更新适配器
     */
    public void setNotifyDataSetChanged() {
        bannerAdapter.notifyDataSetChanged();
    }


    /**
     * 初始化调用
     *
     * @param productDetails
     */


    private ArrayList<Banner.Bannerdata> mBannerList;

    public void setViewPager(ArrayList<Banner.Bannerdata> bannerList) {
        mBannerList = bannerList;
        count = bannerList.size();
        initViewPager();
    }


    private ArrayList<String> mBannerList2;

    public void setViewPager2(ArrayList<String> bannerList2) {
        mBannerList2 = bannerList2;
        count = bannerList2.size();
        initViewPager();
    }
    private ArrayList<AdPictureData.DataBean.AdBean> mBannerList4;
    public void setViewPager4(ArrayList<AdPictureData.DataBean.AdBean> bannerList) {
        mBannerList4 = bannerList;
        count = bannerList.size();
        initViewPager();
    }
    private int mType;

    //自动滑动
    public void autoScroll(FragmentManager fragmentManager, ViewPager viewPager, int type) {
        this.mType = type;
        mViewPager = viewPager;
        mFragmentManager = fragmentManager;
        if (mType != 2) {
            if(count>1) {
                mViewPager.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        int currentItem = mViewPager.getCurrentItem();
                        if (!mDragging && isVisible) {
                            mViewPager.setCurrentItem(currentItem + 1);
                        }
                        mViewPager.postDelayed(this, 5000);
                    }
                }, 5000);
                //触摸时不滚动
                mViewPager.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        int action = event.getAction();
                        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
                            mDragging = true;
                        } else if (action == MotionEvent.ACTION_UP) {
                            mDragging = false;
                        }
                        return false;
                    }
                });
            }
        } else {

            radius = 15;
        }
    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager() {
        mViewPager.setCurrentItem(MAX / 2);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.addOnPageChangeListener(new PageChangeListener());
        bannerAdapter = new BannerAdapter(mFragmentManager);
        mViewPager.setAdapter(bannerAdapter);
    }

    //Page监听
    private class PageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int arg0) {

        }

        @Override
        public void onPageScrolled(int pos, float x, int arg2) {
            pos %= count;
                move(pos, x);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }

    private class BannerAdapter extends FragmentStatePagerAdapter {
        private BannerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return MAX;
        }
        @Override
         public int getItemPosition(Object object) {
                return PagerAdapter.POSITION_NONE;
             }
        @Override
        public Fragment getItem(int pos) {
            int index = pos % count;
            if (mType == 1) {
                if (mBannerList != null) {
                    return BannerItemFragment.newInstance(mBannerList.get(index).getThumb(), mBannerList.size(), mBannerList.get(index).getLink());
                } else {
                    return null;
                }
            } else if (mType == 3) {

                //店铺的轮播图
                if (mBannerList2 != null) {
                    return BannerItemFragment.newInstance(mBannerList2.get(index), mBannerList2.size(), mBannerList2.get(index));
                } else {
                    return null;
                }

            }
            else if (mType == 4) {

                //定制的轮播图
                if (mBannerList4 != null) {

                    return BannerItemFragment.newInstance(mBannerList4.get(index).getPath(), mBannerList4.size(), mBannerList4.get(index).getAnurl());
                } else {
                    return null;
                }
            }
            else {

                return null;
            }


        }
    }
}
